<template>
  <div class="app-container">
    <el-tabs @tab-click="changeTab()" v-model="activeName" type="card">
      <el-tab-pane label="定金预售列表" name="0">
        <div class="filter-container">
          <el-input placeholder="促销名称" v-model="listQuery.name" maxlength="32" size="medium" style="width: 200px;"
                    class="filter-item"/>
          <el-button class="filter-item" type="primary" size="medium" @click="handleQuery" icon="el-icon-search">搜索
          </el-button>
          <br/>
          <router-link style="margin-left: 10px" to="/marketingManager/marketingGoods/adddepositpresale">
            <el-button class="filter-item" size="medium" type="primary" icon="el-icon-plus">添加定金预售</el-button>
          </router-link>
          <el-button class="filter-item" size="medium" style="margin-left: 5px;" type="danger" icon="el-icon-delete"
                     @click="batchDelete()">批量删除
          </el-button>
        </div>

        <el-table
          :data="list"
          v-loading="listLoading"
          border
          fit
          highlight-current-row
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" align="center" width="40"/>
          <el-table-column label="促销名称">
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column label="描述">
            <template slot-scope="scope">{{ scope.row.desc }}</template>
          </el-table-column>
          <el-table-column label="尾款支付时间">
            <template slot-scope="scope">{{ scope.row.endTime }}</template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <router-link :to="{path:'/marketingManager/marketingGoods/updatedepositpresale',query:{id:scope.row.id}}">
                <el-button type="primary" style="margin: 0 5px 0 0" size="mini">修改</el-button>
              </router-link>
              <el-button size="mini" style="margin: 0" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="全款预售列表" name="1">
        <div class="filter-container">
          <el-input placeholder="促销名称" v-model="listQuery.name" maxlength="32" size="medium" style="width: 200px;"
                    class="filter-item"/>
          <el-button class="filter-item" type="primary" size="medium" @click="handleQuery" icon="el-icon-search">搜索
          </el-button>
          <br/>
          <router-link style="margin-left: 10px" to="/marketingManager/marketingGoods/addfullpresale">
            <el-button class="filter-item" size="medium" type="primary" icon="el-icon-plus">添加全款预售</el-button>
          </router-link>
          <el-button class="filter-item" size="medium" style="margin-left: 5px;" type="danger" icon="el-icon-delete"
                     @click="batchDelete()">批量删除
          </el-button>
        </div>

        <el-table
          :data="list"
          v-loading="listLoading"
          border
          fit
          highlight-current-row
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" align="center" width="40"/>
          <el-table-column label="促销名称">
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column label="描述">
            <template slot-scope="scope">{{ scope.row.desc }}</template>
          </el-table-column>
          <el-table-column label="发货时间">
            <template slot-scope="scope">{{ scope.row.endTime }}</template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <router-link :to="{path:'/marketingManager/marketingGoods/updatefullpresale',query:{id:scope.row.id}}">
                <el-button type="primary" style="margin: 0 5px 0 0" size="mini">修改</el-button>
              </router-link>
              <el-button size="mini" style="margin: 0" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>


      <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="listQuery.pageNum+1" :page-sizes="[10,20,30,50]" :page-size="listQuery.pageSize"
                       layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-tabs>
     <!-- 添加或修改分类对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
          <el-form ref="form" :model="form"  label-width="80px">
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入分类名称" />
            </el-form-item>


            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
  </div>
</template>

<script>
  import {
    queryDepositPresale,
    queryFullPresale,queryPreSaleCates,deletePreSaleCate,queryPreSaleCate,addDepositPreSale,updateDepositPreSale,
    deleteDepositPresaleByIds,
    deleteFullPresaleByIds
  } from '@/api/marketing/presale';

  export default {
    data() {
      return {
        activeName: '0',
        selectIds: [],
           // 非多个禁用
              multiple: true,
         // 表单参数
              form: {},
         // 弹出层标题
              title: "",
              // 是否显示弹出层
              open: false,
        isProcess: false, // 接口是否处理中
        listLoading: true, // 加载中
        list: null, // 促销数据
        total: null, // 数据总数
        listQuery: {
          pageNum: 0,
          pageSize: 10,
          name: '',
        }, // 查询参数
      }
    },
    created() {
      if (this.$route.query.type) {
        this.activeName = this.$route.query.type;
      }
      this.getList()
    },
    methods: {
      changeTab() {
        this.listQuery = {
          pageNum: 0,
          pageSize: 10,
          name: '',
        };
        this.selectIds = [];
        this.getList();
      },
      getList() {
        this.listLoading = true;
        if (this.activeName == '0') {
          queryDepositPresale(this.listQuery).then((response) => {
            this.list = response.data;
            this.total = response.recordsTotal;
            this.listLoading = false
          })
        } else  if (this.activeName == '1'){
          queryFullPresale(this.listQuery).then((response) => {
            this.list = response.data;
            this.total = response.recordsTotal;
            this.listLoading = false
          })
        } else  if (this.activeName == '2'){
                   queryPreSaleCates(this.listQuery).then((response) => {
                     this.list = response.data;
                     this.total = response.recordsTotal;
                     this.listLoading = false
                   })
                 }
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val - 1;
        this.getList();
      },
      handleSelectionChange(val) {
        if (val && val.length > 0) {
          this.selectIds = val.map(row => row.id);
        } else {
          this.selectIds = [];
        }
         this.multiple = !val.length
      },
      // 过滤查询
      handleQuery() {
        this.listQuery.pageNum = 0;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 0;
        this.listQuery.pageSize = val;
        this.getList();
      },
      // 删除二次确认
      handleDelete(id) {
        this.$confirm('是否确认删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delMarketing(id);
        })
      },
      // 批量删除二次确认
      batchDelete() {
        if (this.selectIds && this.selectIds.length > 0) {
          this.$confirm('是否确认删除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.delMarketing(this.selectIds);
          })
        } else {
          this.$message({
            message: '请至少选择一条数据',
            type: 'warning'
          })
        }
      },
      // 删除促销
      delMarketing(ids) {
        if (this.isProcess) {
          return;
        }
        this.isProcess = true;
        if (this.activeName == '0') {
          deleteDepositPresaleByIds({"marketingIds": ids.toString()}).then(() => {
            this.isProcess = false;
            this.getList();
            this.selectIds = [];
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          });
        } else {
          deleteFullPresaleByIds({"marketingIds": ids.toString()}).then(() => {
            this.isProcess = false;
            this.getList();
            this.selectIds = [];
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          });
        }
      },
       // 取消按钮
          cancel() {
            this.open = false;
            this.reset();
          },
          // 表单重置
              reset() {
                this.form = {
                  id: undefined,
                  name: undefined,
                  minMoney: undefined,
                  maxMoney: undefined,
                  discount: undefined,
                  delFlag: undefined,
                  createTime: undefined,
                  modifyTime: undefined,
                  delTime: undefined
                };
                this.resetForm("form");
              },
                /** 新增按钮操作 */
                  handleAdd() {
                    this.reset();
                    this.open = true;
                    this.title = "添加分类";
                  },
                  /** 修改按钮操作 */
                  handleUpdate(row) {

                    const id = row.id || this.ids
                    queryPreSaleCate(id).then(response => {
                      this.form = response;
                      this.open = true;
                      this.title = "修改分类";
                    });
                  },
                  /** 提交按钮 */
                  submitForm: function() {
                    this.$refs["form"].validate(valid => {
                      if (valid) {
                        if (this.form.id != undefined) {
                          updateDepositPreSale(this.form).then(response => {
                            if (response===1) {
                              this.msgSuccess("修改成功");
                              this.open = false;
                              this.getList();
                            }
                          });
                        } else {
                          addDepositPreSale(this.form).then(response => {
                            if (response===1) {
                              this.msgSuccess("新增成功");
                              this.open = false;
                              this.getList();
                            }
                          });
                        }
                      }
                    });
                  },

    }
  }
</script>
